package exam;

import java.io.IOException;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import bean.User;
import exam.port.UserPort;

/**
 * Servlet implementation class Query
 */
@WebServlet("/query.do")
public class Query extends HttpServlet implements Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Query() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
		SqlSession session = sessionFactory.openSession(true);
		UserPort userPort = session.getMapper(UserPort.class);
		List<User> queryAll = userPort.queryAll(null);
		List<User> queryClass = userPort.queryClass();
		request.setAttribute("queryAll", queryAll);
		request.setAttribute("queryClass", queryClass);
		request.getRequestDispatcher("/qurey.ftl").forward(request, response);
		session.close();
	}

}
